// pages/order/order.js
Page({


  data: {   address:'',name:'',phone:'',image:'',radio:"1",number:1,tijiao_id:'',addresslist:{},idobj:{},storeobj:{},code:'',
  coupon_id:"",param:"",
  man:'',jian:0,peisong:0,
  radio1:'1',
  iamge:2,
  img:1,
  checkIndex:1

  },
  
 onShow(){
     console.log(this.data.param);
     this.getype()
     console.log(typeof(this.data.jian));
     
     
 },
 

  
  onLoad: function (options) {
   
    
    
       this.setData({
         tijiao_id:options.id,
         
        
      
       
     
     
     })
     
     
     
  },
  priceclick(){
    wx.navigateTo({
      url: "/pages/cancoupon/cancoupon?id="+this.data.tijiao_id,
    })
  },
  //单选框配送不配送
    onChange1(e){
      console.log(e.detail);
      if(e.detail=='1'){this.setData({peisong:1})}else{this.setData({peisong:-1})}
        this.setData({
           radio:e.detail
        })    
       
    },
    //单选框余额支付还是微信支付
    
    onChange2(e){
      console.log(e.detail);
      
           this.setData({
             radio1:e.detail
           })
    

    },
    jianclick(){
      console.log(1);
      
      let value=this.data.number
      
      if(value>1){
        value--
        this.setData({
          number:value
        })
        
      }
    },
    addclick(){
      let value=this.data.number
      value++
      this.setData({
        number:value
      })
    },
    //跳转我的地址
    navclick(){
      wx.navigateTo({
        url: '/pages/address/address',
      })
    }, 
    pick:function(e){
  var type = e.currentTarget.dataset.index;
  this.setData({
    checkIndex:type
  })
 

},

//获取店铺 是否可配送 支付方式
  getype(){
      
        wx.request({
          url: 'https://zujieba.com/Api/car/car.html',
          data:{id:this.data.tijiao_id},
          success:(res)=>{
            console.log(res);
            const addresslist=res.data.data.address
            const idobj=res.data.data.data
            const storeobj=res.data.data.store
            this.setData({
              addresslist,
              idobj,
              storeobj
            })
            if(storeobj.tp==2){
              this.setData({
                radio:'1'
              })
            }
            if(addresslist){
            const address=addresslist.province+addresslist.city+addresslist.area+addresslist.address 
            this.setData({
              address
            })}
            
          }
        })

},
//立即打印 下单
printclcik(){
  
   const uid=wx.getStorageSync('uid')
   const goods_id=this.data.idobj.goods_id
   const spec_id=this.data.idobj.spec_id
   const file=this.data.idobj.file
   const id=this.data.idobj.id
   const store_id=this.data.idobj.store_id
   const pay=this.data.checkIndex  
   const dmcopies=this.data.number
   const deli=this.data.radio
   const coupon_id=this.data.coupon_id
   const params={uid,goods_id,spec_id,pay,coupon_id,pay,dmcopies,deli,address_id:this.data.addresslist.id,file}
   var that =this
   wx.request({
     url: 'https://zujieba.com/Api/order/add_order.html',
     data:params,
     method:'POST',
     success:(res)=>{
       console.log(res);
       if(res.data.success==1){
       that.setData({
         code:res.data.data
       })
    
      wx.request({
        url: 'https://zujieba.com/Api/order/pay_order.html',
        data:{code:res.data.data},
        success:(res)=>{
          console.log(res);
          that.setData({
            appId:res.data.addId,
            nonceStr:res.data.nonceStr,
            package:res.data.package,
            paySign:res.data.paySign,
            signType:res.data.signType,
            timeStamp:res.data.timeStamp,
            
          })
          that.getpay()
          
        }
      })
      
       
     }
     wx.showToast({
      title: res.data.tips,
      icon:"success",
      duration:2000
    })
    }
    

   })

},
  //支付
  getpay(){
    wx.requestPayment(
      {
      'timeStamp': this.data.timeStamp,
      'nonceStr': this.data.nonceStr,
      'package': this.data.package,
      'signType': this.data.signType,
      'paySign': this.data.paySign,
      'success':(res)=>{
              wx.showToast({
                title: '支付成功',
                icon:'success',
                duration:2000
              })
              wx.navigateTo({
                url: '/pages/canprint/canprint?code='+this.data.code,
              })
             
        
      },
      
      })
  },
    

 


 
})